'use strict';
app.service('functionService',['$http','$q','utilService',function($http,$q,utilService){

    this.findByPlatform = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'function/'+ $scope.platform,
            params:{platform:$scope.platform}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }

    this.create = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'function',
            data:$scope.function
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.edit = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'function/'+ $scope.function.id,
            data:$scope.function,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.delete = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'function/'+ $scope.function.id,
            data:{id:$scope.function.id},
            method:'DELETE'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }
}]);